package com.liming.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.liming.model.Banner;
import com.liming.utils.DataSourceUtils;

/**
 * 
 * @author chengw
 * @version 20190628
 */
public class Bannerdao {

	private QueryRunner qRunner =  new QueryRunner(DataSourceUtils.getDataSource());;
	
	public List<Banner> getEnableBanners() throws SQLException {
		// TODO Auto-generated method stub
		return qRunner.query("select * from banner where bannerenable = 1", new BeanListHandler<>(Banner.class));
	}

	public List<Banner> getAllBanners() throws SQLException {
		// TODO Auto-generated method stub
		return qRunner.query("select * from banner", new BeanListHandler<>(Banner.class));

	}

	public void addBanner(Banner banner) throws SQLException {
		// TODO Auto-generated method stub
		qRunner.update("insert into banner(goodid,bannersrc,bannertitile,bannersubtitle,bannerdesc,bannerenable) values ?,?,?,?,?,?",banner.getGoodid(),banner.getBannersrc(),banner.getBannertitle(),banner.getBannersubtitle(),banner.getBannerdesc(),banner.getBannerenable());

	}

	public void disableBanner(int bannerid) throws SQLException {
		// TODO Auto-generated method stub
		qRunner.update("update banner bannerenable = 0 where bannerid = ?",bannerid);		
	}

	public void enableBanner(int bannerid) throws SQLException {
		// TODO Auto-generated method stub
		qRunner.update("update banner bannerenable = 1 where bannerid = ?",bannerid);	
	}
	
}
